package com.kly.cashmall.utils.function;

import android.Manifest;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;

import com.kly.cashmall.utils.data.TextUtil;
import com.kly.cashmall.utils.system.PermissionUtils;

import java.io.File;
import java.math.BigDecimal;

/**
 * @author zxp
 * @since 27/10/21
 */
public class CommonUtils {
    /**
     * 获取指定文件夹内所有文件大小的和
     *
     * @param file file
     * @return size
     * @throws Exception
     */
    public static long getFolderSize(File file) throws Exception {
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            for (File aFileList : fileList) {
                if (aFileList.isDirectory()) {
                    size = size + getFolderSize(aFileList);
                } else {
                    size = size + aFileList.length();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }

    /**
     * 格式化单位
     *
     * @param size size
     * @return size
     */
    public static String getFormatSize(double size) {

        double kiloByte = size / 1024;
        if (kiloByte < 1) {
            return size + "Byte";
        }

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
        }

        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);

        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
    }

    public static String getPicType(String pathName) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathName, options);
        String type = options.outMimeType;
        if (!TextUtils.isEmpty(type)) {
            try {
                type = type.substring(6, type.length());
                if ("gif".equals(type)) {
                    return ".gif";
                }
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
            }
        }
        return ".jpg";
    }

    public static boolean isSDCardExsit() {
        String state = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(state);
    }

    // 两次点击按钮之间的点击间隔不能少于1000毫秒
    private static final int MIN_CLICK_DELAY_TIME = 2000;
    private static long lastClickTime;

    public static boolean isFastClick() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }

    public static String getDeviceId(Context context) {
        String deviceId;
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        } else {
            TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (mTelephony != null) {
                if (!PermissionUtils.lacksPermission(context, Manifest.permission.READ_PHONE_STATE)) {
                    deviceId = mTelephony.getDeviceId();
                }else {
                    deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
                }
            } else {
                deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            }
        }

        return deviceId;
    }

    public static int lowerNum = 0;
    public static boolean isLower(String str){
        for (int i = str.length();--i>=0;){
            if(Character.isLowerCase(str.charAt(i))||Character.isUpperCase(str.charAt(i))) {
                lowerNum++;
            }
        }
        if (lowerNum >= 4){
            lowerNum = 0;
            return true;
        }else {
            lowerNum = 0;
            return false;
        }
    }


    public static SpannableString setSymbol(String text, String narrowText, float narrowSize){
        SpannableString spannableString = new SpannableString(text);
        if (!TextUtil.isEmpty(narrowText)) {
            spannableString.setSpan(new RelativeSizeSpan(narrowSize), text.lastIndexOf(narrowText), text.lastIndexOf(narrowText) + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }
    public static SpannableString setHighLight(String text, String narrowText, int color){
        SpannableString spannableString = new SpannableString(text);
        if (!TextUtil.isEmpty(narrowText)) {
            spannableString.setSpan(new ForegroundColorSpan(color),text.indexOf(narrowText),text.indexOf(narrowText)+narrowText.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }
    public static SpannableString setHighLightTwo(String text, String narrowText, String narrowTextTwo, int color){
        SpannableString spannableString = new SpannableString(text);
        if (!TextUtil.isEmpty(narrowText)) {
            spannableString.setSpan(new ForegroundColorSpan(color),text.indexOf(narrowText),text.indexOf(narrowText)+narrowText.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new ForegroundColorSpan(color),text.indexOf(narrowTextTwo),text.indexOf(narrowTextTwo)+narrowTextTwo.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }
    public static SpannableString setHighLightTwoTextSize(String text, String narrowText, String narrowTextTwo, int color,float narrowSize){
        SpannableString spannableString = new SpannableString(text);
        if (!TextUtil.isEmpty(narrowText)) {
            spannableString.setSpan(new ForegroundColorSpan(color), text.indexOf(narrowText), text.indexOf(narrowText) + narrowText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(narrowSize), text.lastIndexOf(narrowText), text.lastIndexOf(narrowText) + narrowText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (!TextUtil.isEmpty(narrowTextTwo)) {
            spannableString.setSpan(new ForegroundColorSpan(color),text.indexOf(narrowTextTwo),text.indexOf(narrowTextTwo)+narrowTextTwo.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(narrowSize), text.lastIndexOf(narrowTextTwo), text.lastIndexOf(narrowTextTwo) +narrowTextTwo.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        }
        return spannableString;
    }

    public static SpannableString setHighLightAndSymbol(String text, String narrowText, int color,String symbol,float narrowSize){
        SpannableString spannableString = new SpannableString(text);
        if (!TextUtil.isEmpty(narrowText)) {
            spannableString.setSpan(new RelativeSizeSpan(narrowSize), text.lastIndexOf(symbol), text.lastIndexOf(symbol) + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new ForegroundColorSpan(color),text.indexOf(narrowText),text.indexOf(narrowText)+narrowText.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }
}
